//
//  QYXNetTool.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXNetTool.h"

@implementation QYXNetTool
#pragma mark -- Get数据请求
+ (void)getNetWithUrl:(NSString *)url urlBody:(id)body header:(NSDictionary *)header response:(QYXResponseStyle)response success:(BLOCKOFSuccess)success failure:(BLOCKOFFailure)failure
{
    //创建网络管理者:
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //请求头的设置:
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //设置返回数据的类型:
    switch (response) {
        case QYXJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case QYXXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case QYXDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //设置响应数据类型:
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //转码:iOS9-----新改的
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //发送请求:
    [manager GET:url parameters:body progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        NSLog(@"%@", error);
    }];
}
#pragma mark -- Post数据请求
+ (void)postNetWithUrl:(NSString *)url urlBody:(id)body bodyStyle:(QYXRequestStyle)bodyStyle header:(NSDictionary *)header response:(QYXResponseStyle)response success:(BLOCKOFSuccess)success failure:(BLOCKOFFailure)failure
{
    //创建网络管理者:
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置body数据类型:
    switch (bodyStyle) {
        case QYXBodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case QYXBodyString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    //请求头的设置:
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //设置返回数据的类型:
    switch (response) {
        case QYXJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case QYXXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case QYXDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    //设置响应数据类型:
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    //转码:iOS9-----新改的
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //请求https所需加程序:
    manager.securityPolicy.allowInvalidCertificates = YES;
    //发送请求:
    [manager POST:url parameters:body progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        NSLog(@"%@", error);
    }];
}

@end
